/****************************************Copyright (c)**************************************************
**                                         2016-03
**
**                                
**
**
**--------------File Info-------------------------------------------------------------------------------
** File name:		    ht6x1x_flash.c
** Last modified Date:  2016-03-29
** Last Version:		v1.0
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Created by:			Evan Chen
** Created date:		2016-03-29
** Version:				v1.0
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "ht6x1x_flash.h"

void WPREG_Disable(void)
{
     HT_CMU->WPREG  = 0xA55A;
}

void WPREG_Enable(void)
{
    HT_CMU->WPREG  = 0;
}

FunctionalState WPREG_State(void)
{
    if(HT_CMU->WPREG & 0x0001)return ENABLE;
	
    return DISABLE;
}

void FLASH_Unlock(void)
{
    HT_CMU->FLASHLOCK = 0x7A68;
}

void FLASH_Lock(void)
{
    HT_CMU->FLASHLOCK = 0;
}

void FLASH_INFO_Unlock(void)
{
    HT_CMU->INFOLOCK = 0xF998;
}

void FLASH_INFO_Lock(void)
{
    HT_CMU->INFOLOCK = 0;
}

FunctionalState FLASH_State(void)
{
    if(HT_CMU->FLASHLOCK & 0x0001)return ENABLE;
	
    return DISABLE;
}

FLASH_Status FLASH_ErasePage(uint32_t FLASH_Page)
{
      if(FLASH_Page >= FLASH_SIZE)return FLASH_ERROR;
	  
     WPREG_Disable();
     FLASH_Unlock();

     HT_CMU->FLASHCON = FLASHCON_FOP_ERASE_PAGE;	 
     M32(FLASH_Page)  = 0;
     while(HT_CMU->FLASHCON & FLASHCON_BUSY_MASK);

     FLASH_Lock();
     WPREG_Enable();
	 
     return FLASH_COMPLETE;
}

FLASH_Status FLASH_EraseAllPage(void)
{
     WPREG_Disable();
     FLASH_Unlock();

     HT_CMU->FLASHCON = FLASHCON_FOP_ERASE_ALL;	 
     M32(0)  = 0;
     while(HT_CMU->FLASHCON & FLASHCON_BUSY_MASK);

     FLASH_Lock();
     WPREG_Enable();
	 
     return FLASH_COMPLETE;
}

FLASH_Status FLASH_Program_M32(uint32_t Address, uint32_t Data)
{
     if(Address > (FLASH_SIZE - 4))return FLASH_ERROR;
	 
     HT_CMU->FLASHCON = FLASHCON_FOP_WRITE;	 
     M32(Address)  = Data;
     while(HT_CMU->FLASHCON & FLASHCON_BUSY_MASK);

     return FLASH_COMPLETE;
}

FLASH_Status FLASH_Program_M16(uint32_t Address, uint16_t Data)
{
     if(Address > (FLASH_SIZE - 2))return FLASH_ERROR;
	 
     HT_CMU->FLASHCON = FLASHCON_FOP_WRITE;	 
     M16(Address)  = Data;
     while(HT_CMU->FLASHCON & FLASHCON_BUSY_MASK);

     return FLASH_COMPLETE;
}

FLASH_Status FLASH_Program_M8(uint32_t Address, uint8_t Data)
{
     if(Address > (FLASH_SIZE - 1))return FLASH_ERROR;

     HT_CMU->FLASHCON = FLASHCON_FOP_WRITE;	 
     M8(Address)  = Data;
     while(HT_CMU->FLASHCON & FLASHCON_BUSY_MASK);
	 
     return FLASH_COMPLETE;
}
